package chatGUI;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;

/* 
 * @author Yahya-Albaba
*/

public class ConnectionWindow extends JFrame implements ActionListener {
    
    protected DefaultListModel usersListModel;
    protected  JList usersList;
    protected  JButton logIn;
    protected  JTextField usernameBox;
    protected JTextArea chatBox;
    protected Calendar cal;
    protected  JScrollPane chatPane, inputPane, listPane, userInputPane, userChatPane;
    protected JPanel  window;
    protected  JLabel loginLabel, listLabel, infoLabel;
    protected JTextArea inputArea;
    protected  JFrame frame;
    protected   JLabel labUsername;
    protected  ChatGui cg;
    protected String formattedUsername;
    private ChatWindow cw;


    /**
    *This class allows use open the chat window 
    *
    */ 
	public ConnectionWindow(ChatGui cg){
   	 	super();
        this.cg = cg;
        this.display();
   }
   
    
    
    public void display() {
    	
         
       frame = new JFrame("Chat System");
        JPanel southPanel = new JPanel();

        frame.getContentPane().add(BorderLayout.NORTH, southPanel);
        southPanel.setBackground(Color.gray);
        southPanel.setLayout(new GridBagLayout());

        usernameBox = new JTextField(30);
        //usernameBox=new JTextField(10);
        //usernameBox.setName(" Username ");
       // usernameBox.setEditable(false);
        
        logIn = new JButton("Connect");
    
       
 
        Font font = new Font("Arial", Font.BOLD, 15);
  
        
        
        GridBagConstraints left = new GridBagConstraints();
        left.anchor = GridBagConstraints.WEST;
        GridBagConstraints right = new GridBagConstraints();
        right.anchor = GridBagConstraints.EAST;
        right.weightx = 2.0;
        labUsername = new JLabel("Username : ");
        southPanel.add(labUsername);
        southPanel.add(usernameBox);
      //  southPanel.add(usernameBox);
     
        
        southPanel.add(logIn);

        logIn.addActionListener(this);
        
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(670, 500);
    }

    public void actionPerformed(ActionEvent event) {
        if (usernameBox.getText().length() < 1) {
            // do nothing 
        } else {
        	 if (!"".equals(usernameBox.getText())) {
               System.out.println("connection of " + usernameBox.getText());
               formattedUsername = usernameBox.getText().trim();
               cg.connect(formattedUsername);
               System.out.println("on doit envoyer un hello"); 
        	 }
        	frame.setVisible(false);
        
        }
    }
    
    public ChatWindow getCw() {
		return cw;
	}



	public void setCw(ChatWindow cw) {
		this.cw = cw;
	}
    
 
}